package com.rayboo.example.jpa;

import com.rayboo.example.random.ShortUUID;
import org.hibernate.MappingException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.IdentityGenerator;

import java.io.Serializable;

/**
 * Jpa自定义ID生成器
 */
public class CustomIDGenerator extends IdentityGenerator {

    public Serializable generate(SessionImplementor session, Object object) throws MappingException {
        Object id = ShortUUID.generateShortUUID();
        if (id != null) {
            return (Serializable) id;
        }
        return super.generate(session, object);
    }



}
